package com.basic.util;

import java.io.File;
import java.io.IOException;

public class FileUtil {
	
	public static class FileExistException extends Exception {
		private static final long serialVersionUID = 1L;
		private static final String ExceptionTag = "FileException";
	}
	
	public static class DirCreateFailException extends Exception{
		private static final long serialVersionUID = 2L;
		private static final String ExceptionTag = "DirException";
	}
	
	public static File makeFile(String path) throws IOException{
		return makeFile(path.substring(0, path.lastIndexOf(File.separator)), path.substring(path.lastIndexOf(File.separator), path.length()));
	}
	
	public static File makeFile(String dir, String filename) throws IOException{
		File dirF = new File(dir);
		dirF.mkdirs();
		if(dirF.isDirectory()){
			File file = new File(dir+filename);
			if(file.createNewFile()){
				return file;
			}
		}
		return null;
	}
	
	public static void makeDirs(String path) throws IOException {
		String sp = java.io.File.separator;
		String[] dirs = path.split(sp);
		String root = "";
		for (String p : dirs) {
			String dir;
			if (root.endsWith(sp)) {
				dir = root + p;
			} else {
				dir = root += sp + p;
			}
			root = dir;
			if (createADir(dir) == false) {
				throw new IOException(String.format("create %s failed", dir));
			}
		}
	}
	
	public static boolean createADir(String path) {
		File dir = new File(path);
		if (!dir.exists() || dir.isFile()) {
			return dir.mkdir();
		} else {
			return true;
		}
	}
	
	public static String getFileName(String path) {
		String name = getFileNameWithExt(path);
		int index = name.lastIndexOf(".");
		if(index > -1) {
			name = name.substring(0, index);
		}
		return name;
	}
	
	//获取文件名以及后缀名
	public static String getFileNameWithExt(String path) {
		if(path == null) return null;
		int index = path.lastIndexOf("/");
		String name = null;
		if(index > -1) {
			name = path.substring(index + 1, path.length());
		} else {
			name = path;
		}
		return name;
	}
	
	//获取所在目录
	public static String getFilePath(String file) {
		if(file == null) return null;
		int index = file.lastIndexOf("/");
		if(index > -1) {
			return file.substring(0, index);
		}
		return null;
	}
	
	//获取后缀名
	public static String getFileExt(String path) {
		if(path == null) return null;
		int index = path.lastIndexOf(".");
		if(index >= 0) {
			return path.substring(index + 1, path.length()).toLowerCase();
		}
		return null;
	}
	
	public static boolean isExist(String path) {
		return new File(path).exists();
	}
	
	/**
	 * 重命名文件
	 * @param filePath
	 * @param newName
	 * @throws FileExistException
	 */
	public static void rename(String filePath, String newName) throws FileExistException {
		if(filePath == null || newName == null) return;
		File fe = new File(filePath);
		String newPath = (fe.getParent()).concat(java.io.File.separator)
				.concat(newName)
				.concat(".")
				.concat(getFileExt(filePath));
		if(newName.equals(getFileName(filePath))) {
			return;
		}
		File ft = new File(newPath);
		if(ft.exists()) throw new FileExistException();
		fe.renameTo(ft);
	}
	
	public static boolean delete(String path) {
		if(path == null) return false;
		File file = new File(path);
		if(file.exists() == false) return false;
		String[] subFiles = file.list();
		if(subFiles != null) {
			for(String subName : subFiles) {
				File f = new File(path, subName);
				if(f.isDirectory()) {
					delete(f.getPath());
				} else {
					System.out.println("delete file : " + f.getPath());
					f.delete();
				}
			}
		}
		System.out.println("delete file : " + file.getPath());
		return file.delete();
	}

}
